var pageNum;
$(function () {
    //页面加载完成获取第一页数据
    getTeacherList(1);
    $("body").on('click','#btn1,#btn2',function () {
        //点击搜索查询从第一页开始查询
        getTeacherList(1);
    });
    $("body").on("click",".fa-unlock-alt",function () {//冻结
        var obj = $(this).parent('a');
        var teacherId = obj.data("id");
        updateTeacherStatus(teacherId,2);
    }).on("click",".fa-history",function () {//恢复
        var obj = $(this).parent('a');
        var teacherId = obj.data("id");
        updateTeacherStatus(teacherId,1);
    }).on("click",".fa-trash-o",function () {//删除
        var obj = $(this).parent('a');
        var teacherId = obj.data("id");
        updateTeacherStatus(teacherId,3);
    }).on("click","#lockBatch",function () {//批量冻结
        var teacherIds = obtainIds(2);
        if(!$.isEmptyObject(teacherIds)){
            updateTeacherStatus(teacherIds,2);
        }
    }).on("click","#regainBatch",function () {//批量恢复
        var teacherIds = obtainIds(1);
        if(!$.isEmptyObject(teacherIds)){
            updateTeacherStatus(teacherIds,1);
        }
    }).on("click","#deleteBatch",function () {//批量删除
        var teacherIds = obtainIds(3);
        if(!$.isEmptyObject(teacherIds)){
            updateTeacherStatus(teacherIds,3);
        }
    });
    /**
     * 获取讲师列表页面
     * @param pageNo 当前页码
     */
    function getTeacherList(pageNo) {
        var params = $("#search-form").serializeJSON();
        params = JSON.stringify(params);
        var obj = {
            url: baseloaction + '/admin/manage/teacher/ajaxQueryTeacherList',//请求URL
            contextId:'teacher-list-context',//响应内容页面设置内容ID
            params:params,//请求参数
            pageNo:pageNo,//页码
            pageCall:getTeacherList, //分页回调加载数据(点击上下页时调用,该方法也在加载页面完成时调用)
            selectAllId:'select-all-teacher', //全选框ID
            callback:function (pageNo) {//数据加载完成回调
                pageNum = pageNo;
                lazyTeacherImage();
            }
        };
        ajaxGetListData(obj);
    }
    /**
     * 修改讲师状态
     * @param teacherIds 讲师ID串
     * @param status 状态，1正常，2禁用，3删除
     */
    function updateTeacherStatus(teacherIds,status) {
        var message ="";
        if(status==1){
            message = "确认要恢复选中讲师数据？";
        }
        if(status==2){
            message = "确认要冻结选中讲师数据？";
        }
        if(status ==3){
            message = "确认要删除选中讲师数据？";
        }
        icModale(2, message, 3, function () {
            $.ajax({
                url: baseloaction + "/admin/manage/teacher/updateSysTeacherStatus",
                data:{"ids":teacherIds,"status":status},
                type:"post",
                dataType:"json",
                success:function (result) {
                    icModale(0,result.message);
                    if(result.success){
                        var arr = (teacherIds+"").split(",");
                        $.each(arr,function (index,id) {
                            if(id!=null && $.trim(id)!=''){
                                var statusEm = $("#teacher-status-"+id);
                                var statusBnt = $("#teacher-updateStatus-btn-"+id);
                                if(1== status){
                                    statusEm.html('<span class="label label-info">正常</span>');
                                    statusBnt.attr('data-original-title','冻结');
                                    statusBnt.find(".fa").removeClass('fa-history').addClass('fa-unlock-alt');
                                }else if(2 == status){
                                    statusEm.html('<span class="label label-danger">冻结</span>');
                                    statusBnt.attr('data-original-title','恢复');
                                    statusBnt.find(".fa").removeClass('fa-unlock-alt').addClass('fa-history');
                                }else if(3== status){
                                    $("#teacher-"+id).remove();
                                }
                            }
                        });
                    }
                },
                error:function (error) {
                    icModale(0,"系统繁忙,讲师状态操作失败");
                }
            });
        });
    }


    /**
     * 获取勾选ID是否为空
     * @param obj
     * @return {*}
     */
    function obtainIds(status) {
        var obj = $('input[name="teacherId"]:checked');
        if(obj.length <= 0){
            if(status==1){
                icModale(0,"请选择要恢复的数据");
            }
            if(status==2){
                icModale(0,"请选择要冻结的数据");
            }
            if(status==3){
                icModale(0,"请选择要删除的数据");
            }
            return null;
        }else {
            var teacherIds = "";
            obj.each(function (index, em) {
                teacherIds += em.value + ",";
            })
            teacherIds = teacherIds.substr(0,teacherIds.length-1);
            return teacherIds;
        }
    }
});


function lookTeacherDetail() {
    $("body").on("click",".teacher-check-btn",function () {
    window.location.href = baseloaction + "/admin/sys/teacher/findTeacherById?id=" + $(this).data('id');})
}

